﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using TAR4_Model;

namespace TAR4_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["RestaurantContainer"].ConnectionString;
            RestaurantContainer c = new RestaurantContainer(connectionString);


            IQueryable<Product> products = c.ProductSet.Where(o => o.Name.Contains("A"));
            foreach (Product product in products)
            {
                product.Name = "Prodotto B";
                // Console.WriteLine("Prodotto: {0}", product.Name);
            }

            //DeliverySite newDeliverySite = new DeliverySite() { Description = "Punto distribuzione prova", Name = "PuntoDistribuzione", Type = "Tipo1" };
            //c.AddObject("DeliverySiteSet", newDeliverySite);

            //Product newProduct = new Product()
            //                         {
            //                             Buono = true,
            //                             Code = "A0001",
            //                             DeliverySite = newDeliverySite,
            //                             Description = "Prodotto A descrizione",
            //                             ShortName = "PA",
            //                             Name = "Prodotto A"
            //                         };
            //c.AddObject("ProductSet", newProduct);


            c.SaveChanges();
            c.Dispose();

            Console.ReadLine();
            Console.ReadLine();
























            return;









            // Aggiungi un punto di distribuzione
            //c.AddObject("DeliverySiteSet",
            //            new DeliverySite() { Description = "Ristorante", Name = "Ristorante", Type = "T1" });

            // Aggiungi dei prodotti
            //DeliverySite deliverySite = c.DeliverySiteSet.First(o => o.ID == 1);
            //c.AddObject("ProductSet", new Product() { Code ="A0001", Buono = false, Description = "Pasta al ragù", Name = "Pasta al ragù", ShortName = "P.Ragù", DeliverySite = deliverySite});
            //c.AddObject("ProductSet", new Product() { Code ="A0002", Buono = false, Description = "Pasta al pomodoro", Name = "Pasta al pomodoro", ShortName = "P.Pomo", DeliverySite = deliverySite});
            //c.AddObject("ProductSet", new Product() { Code ="A0003", Buono = false, Description = "Pasta allo scoglio", Name = "Pasta allo scoglio", ShortName = "P.Scoglio", DeliverySite = deliverySite});
            //c.AddObject("ProductSet", new Product() { Code ="A0004", Buono = false, Description = "Pasta alla puttanesca", Name = "Pasta alla puttanesca", ShortName = "P.Ptn", DeliverySite = deliverySite});
            //c.AddObject("ProductSet", new Product() { Code ="A0005", Buono = false, Description = "Pasta al tonno", Name = "Pasta al tonno", ShortName = "P.Tonno", DeliverySite = deliverySite});

            // Associa prodotti al menu del giorno
            Menu currentMenu = c.MenuSet.FirstOrDefault(o => o.StartValidity < DateTime.Now && o.EndValidity > DateTime.Now);
            if (currentMenu == null)
            {
                // Crea un nuovo menu per la giornata corrente
                currentMenu = new Menu()
                                  {
                                      Description = "Giorno 1",
                                      StartValidity = DateTime.Now.Date,
                                      EndValidity = DateTime.Now.AddDays(1).AddMilliseconds(-3),
                                      Name = "Giorno 1"
                                  };
                c.MenuSet.AddObject(currentMenu);
            }

            foreach (Product product in c.ProductSet)
            {
                if (c.ProductInStocks.Any(o => o.Menu.ID == currentMenu.ID && o.Product.Code == product.Code))
                {
                    // C'è già: incrementa il prezzo di 1
                    c.ProductInStocks.First(o => o.Menu.ID == currentMenu.ID && o.Product.Code == product.Code).
                        Price += 1;
                }
                else
                {
                    // Se il prodotto non è presente...
                    currentMenu.ProductInStock.Add(new ProductInStock() { Product = product, Price = 5 });
                }
            }

            //Menu newMenu = new Menu()
            //                  {
            //                      Description = "Giorno 1",
            //                      StartValidity = DateTime.Now,
            //                      EndValidity = DateTime.Now.AddDays(1),
            //                      Name = "Giorno 1"
            //                  };

            //c.MenuSet.AddObject(newMenu);
            c.SaveChanges();
            c.Dispose();

            //Console.ReadLine();
            //Console.ReadLine();
        }
    }
}
